﻿using System;
using System.Runtime.InteropServices;
using CommonLibrary.Extensions;
using CommonLibrary.Models.ControllerStates.PS3;

/**********************************************
 * 
 *  Code inspired from Matlo's work
 *  http://www.gimx.fr/forum/
 * 
 **********************************************/

namespace ConnectionManager.Models
{
    [StructLayout(LayoutKind.Sequential)]
    [Serializable]
    public class PS3SerialReport
    {
        public UInt16 X;
        public UInt16 Y;
        public UInt16 Z;
        public UInt16 Rz;
        public UInt16 Hat;
        public UInt16 Bt;
    }

    public static class SixaxisPacketBuilder
    {
        public static PS3SerialReport CreateReportData(PS3ControllerState sixaxisState)
        {
            var data = new PS3SerialReport();

            data.X = sixaxisState.User.LAxisX;
            data.Y = sixaxisState.User.LAxisY;
            data.Z = sixaxisState.User.RAxisX;
            data.Rz = sixaxisState.User.RAxisY;

            if (sixaxisState.User.Buttons.Square.Pressed)
            {
                data.Bt |= 0x0001;
            }
            if (sixaxisState.User.Buttons.Cross.Pressed)
            {
                data.Bt |= 0x0002;
            }
            if (sixaxisState.User.Buttons.Circle.Pressed)
            {
                data.Bt |= 0x0004;
            }
            if (sixaxisState.User.Buttons.Triangle.Pressed)
            {
                data.Bt |= 0x0008;
            }
            if (sixaxisState.User.Buttons.Select.Pressed)
            {
                data.Bt |= 0x0100;
            }
            if (sixaxisState.User.Buttons.Start.Pressed)
            {
                data.Bt |= 0x0200;
            }
            if (sixaxisState.User.Buttons.L3.Pressed)
            {
                data.Bt |= 0x0400;
            }
            if (sixaxisState.User.Buttons.R3.Pressed)
            {
                data.Bt |= 0x0800;
            }
            if (sixaxisState.User.Buttons.L1.Pressed)
            {
                data.Bt |= 0x0010;
            }
            if (sixaxisState.User.Buttons.R1.Pressed)
            {
                data.Bt |= 0x0020;
            }
            if (sixaxisState.User.Buttons.L2.Pressed)
            {
                data.Bt |= 0x0040;
            }
            if (sixaxisState.User.Buttons.R2.Pressed)
            {
                data.Bt |= 0x0080;
            }

            if (sixaxisState.User.Buttons.Right.Pressed)
            {
                if (sixaxisState.User.Buttons.Down.Pressed)
                {
                    data.Hat = 0x0003;
                }
                else if (sixaxisState.User.Buttons.Up.Pressed)
                {
                    data.Hat = 0x0001;
                }
                else
                {
                    data.Hat = 0x0002;
                }
            }
            else if (sixaxisState.User.Buttons.Left.Pressed)
            {
                if (sixaxisState.User.Buttons.Down.Pressed)
                {
                    data.Hat = 0x0005;
                }
                else if (sixaxisState.User.Buttons.Up.Pressed)
                {
                    data.Hat = 0x0007;
                }
                else
                {
                    data.Hat = 0x0006;
                }
            }
            else if (sixaxisState.User.Buttons.Down.Pressed)
            {
                data.Hat = 0x0004;
            }
            else if (sixaxisState.User.Buttons.Up.Pressed)
            {
                data.Hat = 0x0000;
            }
            else
            {
                data.Hat = 0x0008;
            }

            return data;
        }
    }
}
